package cz.muni.fi.pa165.travelagency.web.wsapi;

import cz.muni.fi.pa165.travelagency.api.entities.ICustomer;
import cz.muni.fi.pa165.travelagency.api.services.ICustomerService;
import cz.muni.fi.pa165.travelagency.dto.CustomerDTO;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.jws.Oneway;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;

/**
 *
 */
@WebService(serviceName = "CustomerService", wsdlLocation = "CustomerService.wsdl")
public class CustomerService {

    public CustomerService() {
        SecurityUtils.getSubject().login(new UsernamePasswordToken("rest", "rest", true));
    }
    
    @EJB
    private ICustomerService ejbRef;

    @WebMethod(operationName = "createCustomer")
    @Oneway
    public void createCustomer(@WebParam(name = "customer") CustomerDTO customer) {
        ejbRef.createCustomer(customer);
    }

    @WebMethod(operationName = "deleteCustomer")
    @Oneway
    public void deleteCustomer(@WebParam(name = "customer") CustomerDTO customer) {
        ejbRef.deleteCustomer(customer);
    }

    @WebMethod(operationName = "findAllCustomers")
    public List<CustomerDTO> findAll() {
        
        List<CustomerDTO> all = new ArrayList<>();
        for (ICustomer ipt : ejbRef.findAll()) {
            all.add((CustomerDTO) ipt);
        }
        return all;
    }

    @WebMethod(operationName = "findCustomerByName")
    public List<CustomerDTO> findByName(@WebParam(name = "name") String name) {
        List<CustomerDTO> found = new ArrayList<>();
        for (ICustomer ipt : ejbRef.findByName(name)) {
            found.add((CustomerDTO) ipt);
        }
        return found;
    }

    @WebMethod(operationName = "getCustomer")
    public CustomerDTO getCustomer(@WebParam(name = "id") Long id) {
        return (CustomerDTO) ejbRef.getCustomer(id);
    }

    @WebMethod(operationName = "updateCustomer")
    @Oneway
    public void updateCustomer(@WebParam(name = "customer") CustomerDTO customer) {
        ejbRef.updateCustomer(customer);
    }
}
